import json
class CRevTx:
    def __init__(self, txhash="",blkhash="", consumeVout = [],consumeVoutTxhash=[] ):
        #交易hash
        self.__txhash = txhash
        self.__blkhash = blkhash
        self.__consumeVout = consumeVout
        self.__consumeVoutTxhash = consumeVoutTxhash
    
    @property
    def txhash(self):
        return self.__txhash

    @txhash.setter
    def txhash(self,value):
        self.__txhash =value

    @property
    def blkhash(self):
        return self.__blkhash

    @blkhash.setter
    def blkhash(self,value):
        self.__blkhash =value
    
    @property
    def consumeVout(self):
        return self.__consumeVout

    @consumeVout.setter
    def consumeVout(self,value):
        self.__consumeVout =value
    
    @property
    def consumeVoutTxhash(self):
        return self.__consumeVoutTxhash

    @consumeVoutTxhash.setter
    def consumeVoutTxhash(self,value):
        self.__consumeVoutTxhash =value


   #Object——>jsonStr
    def serializeToJsonStr(self):
        sttr=json.dumps(self, default=lambda obj: obj.__dict__)
        return sttr.replace("_CRevTx__","").replace("_CTxOut__","").replace("_CScriptPubKey__","")

    #jsonStr——> Object
    @classmethod
    def deserializeFromJsonStr(cls,jsonStrobjectdata):
        d = json.loads(jsonStrobjectdata)
        #print(d['txins'])
        return  cls(d['txhash'],d['blkhash'],d['consumeVout'],d['consumeVoutTxhash'])
       
    #Object——>Python Object
    def serializeToPyObject(self):
        return json.loads(self.serializeToJsonStr())
    
    #Python Object——>Object
    @classmethod
    def deserializeFromPyObject(cls,pyObject):
       return cls.deserializeFromJsonStr(json.dumps(pyObject))
       
class CRevblock:
    def __init__(self,selfhash="",blkhash="", transactions = [] ):
        #交易hash
        self.__selfhash = selfhash
        self.__blkhash = blkhash
        self.__transactions = transactions
    
    @property
    def selfhash(self):
        return self.__selfhash

    @selfhash.setter
    def selfhash(self,value):
        self.__selfhash =value

    @property
    def blkhash(self):
        return self.__blkhash

    @blkhash.setter
    def blkhash(self,value):
        self.__blkhash =value
    
    @property
    def transactions(self):
        return self.__transactions

    @transactions.setter
    def transactions(self,value):
        self.__transactions = value


   #Object——>jsonStr
    def serializeToJsonStr(self):
        sttr=json.dumps(self, default=lambda obj: obj.__dict__)
        return sttr.replace("_CRevblock__","")

    #jsonStr——> Object
    @classmethod
    def deserializeFromJsonStr(cls,jsonStrobjectdata):
        d = json.loads(jsonStrobjectdata)
        #print(d['txins'])
        return  cls(d['selfhash'],d['blkhash'],d['transactions'])
       
    #Object——>Python Object
    def serializeToPyObject(self):
        return json.loads(self.serializeToJsonStr())
    
    #Python Object——>Object
    @classmethod
    def deserializeFromPyObject(cls,pyObject):
       return cls.deserializeFromJsonStr(json.dumps(pyObject))
    

    def serializeToJsonfile(self,filename):
            with open(filename, 'w', encoding='utf-8') as f:
                #json.dump(self, f, default=lambda obj: obj.__dict__)
                json.dump(self,f, default=lambda obj: {
                'selfhash': obj.__selfhash,
                'blkhash': obj.__blkhash,
                'transactions': obj.__transactions
                })

    @classmethod
    def deserializeFromJsonfile(cls,filename):
        with open(filename,encoding='utf-8') as f:
            d = json.load(f)
            return  cls(d['selfhash'],d['blkhash'],d['transactions'])